//
//  MPOAuthMultipartRequest.m
//  MPOAuthMobile
//
//  Created by Christopher Anderson on 14/03/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "MPOAuthMultipartRequest.h"

@interface MPOAuthURLRequest ()
@property (nonatomic, readwrite, retain) NSURLRequest *urlRequest;
@end

@implementation MPOAuthMultipartRequest

@synthesize fileParameters = _fileParameters;

- (void) addFileParameter:(MPOAuthMultiPartRequestParameter*)inFileParameter {
	[self.fileParameters addObject:inFileParameter];
}

- (id)initWithURL:(NSURL *)inURL andParameters:(NSArray *)inParameters {
	if (self = [super initWithURL:inURL andParameters:inParameters]) {
		self.fileParameters = [NSMutableArray arrayWithCapacity:10];
		self.HTTPMethod = @"POST";
	}
	return self;
}


- (NSURLRequest  *)urlRequestSignedWithSecret:(NSString *)inSecret usingMethod:(NSString *)inScheme {
	[self.parameters sortUsingSelector:@selector(compare:)];
	
	NSMutableURLRequest *aRequest = [[NSMutableURLRequest alloc] init];
	NSMutableString *parameterString = [[NSMutableString alloc] initWithString:[MPURLRequestParameter parameterStringForParameters:self.parameters]];
	[aRequest setHTTPMethod:self.HTTPMethod];
	
	MPOAuthSignatureParameter *signatureParameter = [[MPOAuthSignatureParameter alloc] initWithText:parameterString andSecret:inSecret forRequest:self usingMethod:inScheme];
	
	
	[self addAuthorizationHeaderToRequest:aRequest withSignature:signatureParameter];

		
	NSLog(@"urlString - %@", self.url);
	NSLog(@"postDataString - %@", parameterString);
		
	[aRequest setURL:self.url];	
		
	NSString *stringBoundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
	NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=--%@--",stringBoundary];
	[aRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
		
	NSMutableData *postData = [[NSMutableData alloc] init];
	MPOAuthMultiPartImageRequestParameter *param;
	
	[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	
	for (param in self.fileParameters) {
		[postData appendData:[param formPostData]];
		[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
	}
	
	[aRequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
	
	[aRequest setHTTPBody:postData];
	[parameterString release];
	[signatureParameter release];		
	
	self.urlRequest = aRequest;
	[aRequest release];
	
	return aRequest;
}

- (void) addAuthorizationHeaderToRequest:(NSMutableURLRequest*)aRequest withSignature:(MPOAuthSignatureParameter*)inSignature {
	NSMutableString *authorizationHeader = [NSMutableString stringWithString:@"OAuth "];
	MPURLRequestParameter *param;
	for (param in self.parameters) {
		[authorizationHeader appendFormat:@"%@=\"%@\",", param.name, [param.value stringByAddingURIPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	}
	[authorizationHeader appendFormat:@"%@=\"%@\"", inSignature.name, [inSignature.value stringByAddingURIPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	[aRequest setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];	
}



@end
